package com.globalwave.common.entity;

import java.util.ArrayList;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.globalwave.common.Convertor;

public class VirtualGrid {
	private VirtualEditForm editForm ;
    private VirtualSearchForm searchForm ;
    
    private View view ;
    private List<Field> fields ;
    
	public VirtualEditForm getEditForm() {
		return editForm;
	}
	public void setEditForm(VirtualEditForm editForm) {
		this.editForm = editForm;
	}
	public VirtualSearchForm getSearchForm() {
		return searchForm;
	}
	public void setSearchForm(VirtualSearchForm searchForm) {
		this.searchForm = searchForm;
	}
	public View getView() {
		return view;
	}
	public void setView(View view) {
		this.view = view;
	}
    public List<Field> getFields() {
		return fields;
	}
	public void setFields(List<Field> fields) {
		this.fields = fields;
	}
    


	public void addFields(List<Object> fields) {
		this.fields = new ArrayList<Field>(fields.size());
		
		for (Object object:fields) {
			this.fields.add((Field)object);
		}
	}

    public String asXML() {
    	return asXML(null) ;
    }
    
    public String asXML(String message) {
        Document document = DocumentHelper.createDocument();
        Element root = document.addElement("module") ;
        
        try {
            
            Element grid = root.addElement("grid") ;
        	grid.add(Convertor.object2Xml(this.view, "view")) ;
        	grid.add(fields2Xml(this.fields)) ;

        	if (this.editForm != null) {
	            Element edit = root.addElement("editForm") ;
	            edit.add(Convertor.object2Xml(this.editForm.getView(), "view")) ;
	            edit.add(fields2Xml(this.editForm.getFields())) ;
        	}

        	if (this.searchForm != null) {
	            Element search = root.addElement("searchForm") ;
	            search.add(Convertor.object2Xml(this.searchForm.getView(), "view")) ;
	            search.add(fields2Xml(this.searchForm.getFields())) ;
        	}
        	
        	if (message != null) {
        		root.addElement("message").addText(message) ;
        	}
        } catch (Exception e) {
            root.addElement("message").addText("Conver grid to xml error!" + e.toString()) ;
        }
        
        return root.asXML() ;
    }
    

    public static Element fields2Xml(List<Field> fields) throws Exception {

        Document document = DocumentHelper.createDocument();
        
        Element root = document.addElement("fields") ;
        
        for (Field field:fields) {
            Element e = Convertor.object2Xml(field, null) ;
            
            if (field.getOptions() != null) {
            	if(e.element("options") != null) {
            		e.remove(e.element("options")) ;
            	}
            	e.add(Convertor.map2Options(field.getOptions())) ;
            }
            
            root.add(e) ;
        }
        
        return root ;
    }
    
}
